package me.khrystal.rxnews.module.settings.ui;

import android.content.res.ColorStateList;
import android.os.Build;
import android.view.View;
import android.widget.CheckedTextView;

import com.afollestad.materialdialogs.MaterialDialog;
import com.zhy.changeskin.SkinManager;

import me.khrystal.rxnews.R;
import me.khrystal.rxnews.annotation.ActivityFragmentInject;
import me.khrystal.rxnews.base.BaseActivity;
import me.khrystal.rxnews.module.settings.presenter.ISettingsPresenter;
import me.khrystal.rxnews.module.settings.presenter.ISettingsPresenterImpl;
import me.khrystal.rxnews.module.settings.view.ISettingsView;
import me.khrystal.rxnews.utils.ClickUtils;
import me.khrystal.rxnews.utils.RxBus;
import me.khrystal.rxnews.utils.SpUtil;
import me.khrystal.rxnews.utils.ThemeUtil;
import me.khrystal.rxnews.utils.ViewUtil;

/**
 * usage:
 * author: kHRYSTAL
 * create time: 16/4/13
 * update time:
 * email: 723526676@qq.com
 */
@ActivityFragmentInject(contentViewId = R.layout.activity_settings,
        menuId = R.menu.menu_settings,
        hasNavigationView = true,
        toolbarTitle = R.string.settings,
        toolbarIndicator = R.drawable.ic_list_white,
        menuDefaultCheckedItem = R.id.action_settings)
public class SettingsActivity extends BaseActivity<ISettingsPresenter> implements ISettingsView {

    private CheckedTextView mNightModeCheckedTextView;
    private CheckedTextView mSlideModeCheckedTextView;

    @Override
    protected void initView() {
        mNightModeCheckedTextView = (CheckedTextView)findViewById(R.id.ctv_night_mode);
        mSlideModeCheckedTextView = (CheckedTextView)findViewById(R.id.ctv_slide_mode);

        mNightModeCheckedTextView.setOnClickListener(this);
        mSlideModeCheckedTextView.setOnClickListener(this);

        findViewById(R.id.tv_about).setOnClickListener(this);

        mPresenter = new ISettingsPresenterImpl(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ctv_night_mode:
                if (ClickUtils.isFastDoubleClick()){
                    return;
                }
                final boolean nightModeCheck = !((CheckedTextView)v).isChecked();
                ((CheckedTextView)v).setChecked(nightModeCheck);
                SkinManager.getInstance().changeSkin(nightModeCheck ? "night" : "");
                SpUtil.writeBoolean("enableNightMode",nightModeCheck);

                setTheme(nightModeCheck ? R.style.BaseAppThemeNight_AppTheme
                    : R.style.BaseAppTheme_AppTheme);

                applyTint(mNightModeCheckedTextView);
                applyTint(mSlideModeCheckedTextView);

                mNightModeCheckedTextView.setText(nightModeCheck ? "关闭夜间模式" : "开启夜间模式");
//                更改主题 发送消息销毁其它Activity
                RxBus.get().post("finish",true);
                break;
            case R.id.ctv_slide_mode:

                final boolean slideModeCheck = !((CheckedTextView)v).isChecked();

                mSlideModeCheckedTextView.setText(slideModeCheck ? "关闭侧滑返回" : "开启侧滑返回");

                ((CheckedTextView)v).setChecked(slideModeCheck);
                SpUtil.writeBoolean("disableSlide", !slideModeCheck);
                if (slideModeCheck){
                    String currentSlideMode = SpUtil
                            .readBoolean("enableSlideEdge") ? "边缘侧滑" : "整页侧滑";
                    new MaterialDialog.Builder(this).title("选择侧滑模式(当前为" + currentSlideMode + ")")
                            .items(R.array.slide_mode_items).itemsCallbackSingleChoice(
                            SpUtil.readBoolean("enableSlideEdge") ? 0 : 1,
                            new MaterialDialog.ListCallbackSingleChoice() {
                                @Override
                                public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                    SpUtil.writeBoolean("enableSlideEdge", which == 0);
                                    return true;
                                }
                            }).positiveText("选择").show();
                }
                break;
            case R.id.tv_about:
                new MaterialDialog.Builder(this).content("http://git.oschina.net/khrystal/RxNews").show();
                break;
        }
    }

    @Override
    public void initItemState() {
        applyTint(mNightModeCheckedTextView);
        applyTint(mSlideModeCheckedTextView);

        mNightModeCheckedTextView.setChecked(SpUtil.readBoolean("enableNightMode"));
        mNightModeCheckedTextView.setText(SpUtil.readBoolean("enableNightMode")?"关闭夜间模式":"开启夜间模式");

        mSlideModeCheckedTextView.setChecked(!SpUtil.readBoolean("disableSlide"));
        mSlideModeCheckedTextView.setText(!SpUtil.readBoolean("disableSlide") ? "关闭侧滑返回":"开启侧滑返回");
    }

    private void applyTint(CheckedTextView checkedTextView) {
        ColorStateList indicator = ThemeUtil
                .getColorStateList(this, R.attr.checkTextViewColorStateList);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            checkedTextView.setCheckMarkTintList(indicator);
        } else {
            ViewUtil.tintDrawable(checkedTextView.getCheckMarkDrawable(), indicator);
        }
    }
}
